﻿using System.Windows.Media;
using System.Windows.Media.Imaging;
using ImageProcessor.Shared;

namespace ImageProcessor.Tim
{
    public class BlurVM : ImageProcessorBaseVM
    {
        public BlurVM(MainViewModel mainVM) : base(mainVM)
        {
            BlurRange = 3;
        }

        private ImageSource _resultImage;
        private int _blurRange;

        public ImageSource ResultImage
        {
            get { return _resultImage; }
            private set
            {
                if (_resultImage == value)
                    return;
                _resultImage = value;
                OnPropertyChanged("ResultImage");
            }
        }

        public int BlurRange
        {
            get { return _blurRange; }
            set
            {
                if(_blurRange == value)
                    return;
                _blurRange = value;

                if ((value & 1) != 0)
                {
                    TryUpdate();
                }

                OnPropertyChanged("BlurRange");
            }
        }

        private void TryUpdate()
        {


            if (MainVM.SourceImage != null)
            {
                var writeableBitmap = new WriteableBitmap((BitmapSource) MainVM.SourceImage);
                writeableBitmap.BoxBlur(BlurRange);

                ResultImage = writeableBitmap;
            }
        }

        protected override void Execute(object obj)
        {
            
        }

        protected override bool CanExecute(object obj)
        {
            return MainVM.SourceImage != null;
        }
    }

    
}